import 'dart:math';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tiktokadult/tools_base/widget/net_image_widget.dart';
import 'package:provider/provider.dart';

import 'pre_sale_page.dart';
import 'pre_sale_provider.dart';

// 首页 - 会员中心入口
class PreSaleEntryWidget extends StatefulWidget {
  final ScrollController? scrollController;

  const PreSaleEntryWidget({super.key, this.scrollController});

  @override
  State<PreSaleEntryWidget> createState() => _PreSaleEntryWidgetState();
}

class _PreSaleEntryWidgetState extends State<PreSaleEntryWidget> with SingleTickerProviderStateMixin {
  double rightPos = 6;
  late final _controller = AnimationController(vsync: this, duration: Duration(milliseconds: 900));
  late Animation<double> _rotateAni;
  late Animation<double> _scaleAni;

  @override
  void initState() {
    _scaleAni = Tween<double>(begin: 1, end: 1.02).animate(
      CurvedAnimation(parent: _controller, curve: Interval(0, 0.8)),
    );

    _rotateAni = Tween<double>(begin: 0, end: 2 * pi).animate(
      CurvedAnimation(parent: _controller, curve: Interval(0.8, 1)),
    );
    widget.scrollController?.addListener(() {
      double offset = widget.scrollController?.offset ?? 0;
      if (offset > (24 + 151 + 36 + 110 + 256)) {
        if (rightPos == 6) {
          rightPos = -65;
          if (mounted) {
            setState(() {});
          }
        }
      } else {
        if (rightPos != 6) {
          rightPos = 6;
          if (mounted) {
            setState(() {});
          }
        }
      }
    });
    if (PreSaleProvider().presaleEnterNeedShow()) {
      _controller.repeat(reverse: true);
    }
    super.initState();
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Consumer<PreSaleProvider>(
      builder: (ctx, provider, child) {
        return provider.presaleEnterNeedShow()
            ? GestureDetector(
                onTap: () => Get.toNamed(PreSalePage.routeName),
                child: AnimatedBuilder(
                  animation: _controller,
                  builder: (context, child) => Transform(
                    ///构建Matrix4
                    transform: Matrix4.rotationZ(sin(0.2 * sin(_rotateAni.value))),
                    alignment: Alignment.center,
                    child: NetworkImageLoader(
                      imageUrl: provider.generatePayEnterButton(),
                      width: 65 * _scaleAni.value,
                    ),
                  ),
                ),
              )
            : SizedBox.shrink();
      },
    );
  }
}

//我的 - 活动页入口
class PreSaleVipEntryWidget extends StatefulWidget {
  const PreSaleVipEntryWidget({super.key});

  @override
  State<PreSaleVipEntryWidget> createState() => _PreSaleVipEntryWidgetState();
}

class _PreSaleVipEntryWidgetState extends State<PreSaleVipEntryWidget> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () => Get.toNamed(PreSalePage.routeName),
      child: Consumer<PreSaleProvider>(
        builder: (ctx, provider, child) {
          return provider.presaleEnterNeedShow()
              ? Stack(
                  children: [
                    NetworkImageLoader(
                      imageUrl: provider.generatePayBanner(),
                      width: double.infinity,
                      fit: BoxFit.fill,
                      height: 76,
                    ),
                    if (provider.canPayBalance())
                      Positioned(
                        bottom: 11,
                        left: 84,
                        child: ValueListenableBuilder(
                          valueListenable: provider.deadline,
                          builder: (BuildContext context, String value, Widget? child) {
                            return Text(
                              '剩余 $value',
                              style: TextStyle(color: Colors.white, fontSize: 14, fontWeight: FontWeight.w700),
                            );
                          },
                        ),
                      )
                  ],
                )
              : SizedBox.shrink();
        },
      ),
    );
  }
}
